public class LongestPalindrome {

    public static void main(String[] args) {
        LongestPalindrome longestPalindrome = new LongestPalindrome();
        System.out.println(longestPalindrome.longestPalindrome("babad"));
    }

    public String longestPalindrome(String s) {
        if (s.length() == 1) return s;
        String ans = "";
        for (int i = 0; i < s.length(); i++) {
            int k = i;
            if (ans.length() + i > s.length()) {
                break;
            } else {
                k = Math.max(ans.length() + i - 1, k);
            }
            for (int j = k; j < s.length(); j++) {
                if (s.charAt(i) != s.charAt(j)) continue;
                String substring = s.substring(i, j + 1);
                StringBuffer stringBuffer = new StringBuffer(substring);
                if (substring.equals(stringBuffer.reverse().toString()) && ((j + 1 - i) > ans.length())) {
                        ans = substring;
                }
            }
        }
        return ans;
    }



}
